install.packages("calendR")
library("calendR")
Introducing calendR: Streamlined Calendar Creation in R
Introduction
The “calendR” package comprises a function titled “calendR()” A versatile function designed to simplify calendar creation and customization within the R programming.The beauty of the “calendR()” function lies in its simplicity and flexibility. By default, it generates a calendar for the current year in landscape format (try to run : calendR()).
Here, We will be using two distinct use cases of “calendR()”:
Yearly Calendar: to display holidays (special events)
Monthly calendar (heatmap): to display daily returns of Nifty50
Let us begin with Installing and loading the required package
1) Yearly Calendar
Highlighting Trading Holidays for the year 2024
Creating the dataset for Holidays
Note:
Only special days need to be highlighted, hence all the other values should be ‘NA’
The vector should have a length corresponding to the number of days in the year or month, depending on the type of calendar being generated.
#Creating a vector with NAs, 2024 is a leap year, hence 366 days
<- rep(NA, 366)
year_vector
#Store Dates of Special Event/ Holidays
<-c("26-01-2024", "08-03-2024", "29-03-2024", "26-03-2024",
holiday_dates"11-04-2024", "17-04-2024", "01-05-2024", "17-06-2024",
"17-07-2024", "15-08-2024", "02-10-2024", "01-11-2024",
"15-11-2024", "25-12-2024")
<-as.Date(holiday_dates, format = "%d-%m-%Y")
holiday_dates
#Getting the days (index of the date)
<- yday(holiday_dates) # from library lubridate
day_num
# Adding Holiday names to the respective day
c(day_num)] <- c("Republic Day","Mahashivratri","Holi","Good Friday",
year_vector["Id-Ul-Fitr (Ramadan Eid)","Shri Ram Navmi",
"Maharashtra Day","Bakri Id","Moharram",
"Independence Day/Parsi New Year",
"Mahatma Gandhi Jayanti","Diwali Laxmi Pujan*",
"Gurunanak Jayanti","Christmas")
Plotting the Holiday Calendar
calendR(year = 2024, #Year for which the calendar needs to be created
special.days = year_vector, #Dates that need to be highlighted
special.col = c("#a8e6cf","#dcedc1","#ffd3b6",
"#e5a3a3","#ff8b94","#b8ddf8",
"#c3b8f8","#eaf5fa","#b7f39b",
"#ffe599","#75dbea","#ced8ff",
"#b100fe","#eb1438"), #Color of the special.days, should be equal to the number of days to be colored
legend.pos = "right", #Position for the legend (Holiday Names)
weeknames = c( "M" , "T" , "W" , "T" ,"F" , "S" , "S" ), #Format for Weeknames
mbg.col ="#ffc03e", #Background color of month name
months.col = "blue",
orientation = "landscape") #default is landscape, try "portrait"
2) Monthly Calendar (Heatmap)
Displaying daily returns for the month of March 2024
Dataset:
head(march_return)
Date daily.returns
1 2024-03-01 0.02
2 2024-03-02 NA
3 2024-03-03 NA
4 2024-03-04 0.00
5 2024-03-05 0.00
6 2024-03-06 0.01
Plotting Heatmap-Calendar for the above daily returns data
calendR(year = 2024, #Year for calendar creation
month = 3, #Month for calendar creation
special.days = march_return$daily.returns, #Dates that need to be highlighted
special.col = "#F8FF00", #Color for the special.days
low.col = "#DC143C", #Color for the lowest values of special.days
gradient = TRUE, #Creates heatmap effect
text.pos = day(march_return$Date), #Days for text insertion in the calendar
text = scales::percent(march_return$daily.returns), #Displaying the daily returns (works only for Monthly calendars)
text.col = "#000000", #Text color
title = "Nifty 50 daily returns", #Title
subtitle = "March 2024", #Sub title
days.col = "#000000", #Day of the month color
weeknames.col = "#4682B4", #Weekday name color
title.col = "#191970", #Title color
legend.pos="bottom") #Position for the legend